// LeetCode 53. 最大子序和
//前缀和加贪心
class Solution {
     int dp[500000];
public:
    int maxSubArray(vector<int>& nums) {
        dp[0]=nums[0];
        int minSum=0;
        int ret=INT_MIN;
        ret=max(ret,dp[0]);
        minSum=min(minSum,dp[0]);
        for(int i=1;i<nums.size();i++)
        {
            dp[i]=dp[i-1]+nums[i];
            ret=max(ret,dp[i]-minSum);
            minSum=min(minSum,dp[i]);
        }

        return ret;
    }
};

//动态规划
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        vector<int> dfs(nums.size());
        dfs[0]=nums[0];
        for(int i=1;i<nums.size();i++)
        {
            dfs[i]=max(dfs[i-1],0)+nums[i];
        }
        return ranges::max(dfs);
    }
};

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int f = 0;
        int ans = INT_MIN;
        for (auto e : nums) {
            f = max(f, 0) + e;
            ans = max(ans, f);
        }
        return ans;
    }
};